//
//  FontManager.h
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-22.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "FontManager.h"
#import "EasyGlyph.h"


@implementation FontManager

@synthesize fontWithSize12;
@synthesize fontWithSize17;
@synthesize fontWithSize25;

static FontManager* pInstance = nil;

+ (id)sharedInstance
{
    if (pInstance == nil)
        pInstance = [[FontManager alloc] init];    
    return pInstance;
}

// init fonts
- (id)init
{
    if (self = [super init])
    {
	fontWithSize12 = [[EasyGlyph alloc] init];
	[fontWithSize12 initFont:@"font12.png":@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890.,-=+:;<>?!'\"[]@#$%^&*()/\\":@"3,2,21,22,4,18;32,2,18,22,4,15;62,2,18,22,4,14;92,2,20,22,4,16;122,2,18,22,4,14;152,2,17,22,4,13;181,2,20,22,4,16;211,2,20,22,4,17;241,2,13,22,4,9;271,2,15,22,4,11;301,2,21,22,4,17;330,2,18,22,4,14;360,2,22,22,4,18;390,2,20,22,4,17;420,2,21,22,4,17;449,2,18,22,4,15;479,2,21,22,4,17;509,2,19,22,4,15;539,2,16,22,4,13;569,2,19,22,4,15;598,2,20,22,4,16;628,2,21,22,4,18;658,2,24,22,4,20;688,2,19,22,4,15;718,2,19,22,4,15;747,2,18,22,4,14;777,2,17,22,4,13;807,2,18,22,4,14;837,2,15,22,4,11;867,2,18,22,4,14;896,2,17,22,4,13;926,2,13,22,4,10;956,2,18,22,4,14;986,2,18,22,4,14;3,25,12,22,4,8;32,25,13,22,4,9;62,25,18,22,4,15;92,25,12,22,4,8;122,25,22,22,4,18;152,25,18,22,4,14;181,25,17,22,4,14;211,25,18,22,4,14;241,25,18,22,4,14;271,25,14,22,4,10;301,25,15,22,4,12;330,25,15,22,4,11;360,25,18,22,4,14;390,25,17,22,4,14;420,25,23,22,4,19;449,25,18,22,4,15;479,25,18,22,4,14;509,25,16,22,4,12;539,25,7,22,1,0;569,25,18,22,4,14;598,25,18,22,4,14;628,25,18,22,4,14;658,25,18,22,4,14;688,25,18,22,4,14;718,25,18,22,4,14;747,25,18,22,4,14;777,25,18,22,4,14;807,25,18,22,4,14;837,25,18,22,4,14;867,25,12,22,4,9;896,25,12,22,4,9;926,25,11,22,4,8;956,25,18,22,4,15;986,25,18,22,4,14;3,48,12,22,4,9;32,48,12,22,4,9;62,48,17,22,4,13;92,48,16,22,4,13;122,48,15,22,4,12;152,48,12,22,4,9;181,48,10,22,4,7;211,48,14,22,4,10;241,48,13,22,4,10;271,48,13,22,4,10;301,48,21,22,4,18;330,48,18,22,4,14;360,48,18,22,4,14;390,48,22,22,4,18;420,48,17,22,4,14;449,48,19,22,4,16;479,48,17,22,4,13;509,48,13,22,4,10;539,48,13,22,4,10;569,48,14,22,4,10;598,48,12,22,4,9"];
	
	fontWithSize17 = [[EasyGlyph alloc] init];
	[fontWithSize17 initFont:@"font17.png":@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890.,-=+:;<>?!'\"[]@#$%^&*()/\\":@"6,4,29,30,5,25;49,4,26,30,5,21;92,4,25,30,5,20;135,4,27,30,5,22;178,4,25,30,5,20;221,4,23,30,5,19;264,4,27,30,5,22;307,4,28,30,5,24;350,4,18,30,5,13;393,4,21,30,5,16;436,4,29,30,5,24;479,4,24,30,5,19;522,4,31,30,5,26;565,4,28,30,5,24;608,4,29,30,5,24;651,4,25,30,5,20;694,4,29,30,5,24;737,4,26,30,5,21;780,4,22,30,5,18;823,4,26,30,5,21;866,4,28,30,5,23;909,4,30,30,5,25;952,4,33,30,5,28;6,37,26,30,5,22;49,37,26,30,5,22;92,37,25,30,5,20;135,37,23,30,5,18;178,37,24,30,5,19;221,37,21,30,5,16;264,37,25,30,5,20;307,37,23,30,5,18;350,37,18,30,5,14;393,37,25,30,5,20;436,37,25,30,5,20;479,37,16,30,5,12;522,37,18,30,5,13;565,37,25,30,5,20;608,37,16,30,5,12;651,37,31,30,5,26;694,37,25,30,5,20;737,37,24,30,5,19;780,37,24,30,5,20;823,37,24,30,5,20;866,37,19,30,5,14;909,37,21,30,5,16;952,37,21,30,5,16;6,70,24,30,5,20;49,70,24,30,5,19;92,70,31,30,5,27;135,70,26,30,5,21;178,70,24,30,5,19;221,70,21,30,5,17;264,70,9,30,1,0;307,70,25,30,5,20;350,70,25,30,5,20;393,70,25,30,5,20;436,70,25,30,5,20;479,70,25,30,5,20;522,70,25,30,5,20;565,70,25,30,5,20;608,70,25,30,5,20;651,70,25,30,5,20;694,70,25,30,5,20;737,70,17,30,5,12;780,70,17,30,5,12;823,70,15,30,5,10;866,70,25,30,5,21;909,70,25,30,5,20;952,70,17,30,5,12;6,103,17,30,5,12;49,103,23,30,5,19;92,103,23,30,5,18;135,103,21,30,5,16;178,103,17,30,5,12;221,103,14,30,5,9;264,103,19,30,5,14;307,103,18,30,5,13;350,103,18,30,5,13;393,103,29,30,5,25;436,103,25,30,5,20;479,103,25,30,5,20;522,103,30,30,5,26;565,103,24,30,5,19;608,103,27,30,5,22;651,103,23,30,5,18;694,103,18,30,5,13;737,103,18,30,5,13;780,103,19,30,5,14;823,103,17,30,5,12"];
	
	fontWithSize25 = [[EasyGlyph alloc] init];
	[fontWithSize25 initFont:@"font25.png":@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890.,-=+:;<>?!'\"[]@#$%^&*()/\\":@"9,6,43,43,7,36;70,6,37,43,7,30;131,6,36,43,7,29;192,6,39,43,7,32;253,6,36,43,7,29;314,6,34,43,7,27;375,6,39,43,7,32;436,6,41,43,7,34;497,6,26,43,7,19;558,6,30,43,7,23;619,6,42,43,7,36;680,6,35,43,7,28;741,6,44,43,7,38;802,6,41,43,7,34;863,6,42,43,7,36;924,6,36,43,7,30;9,54,42,43,7,36;70,54,38,43,7,31;131,54,32,43,7,26;192,54,37,43,7,31;253,54,40,43,7,34;314,54,43,43,7,36;375,54,48,43,7,41;436,54,38,43,7,32;497,54,38,43,7,32;558,54,36,43,7,30;619,54,33,43,7,27;680,54,35,43,7,28;741,54,30,43,7,23;802,54,36,43,7,29;863,54,33,43,7,26;924,54,26,43,7,20;9,102,36,43,7,29;70,102,36,43,7,30;131,102,23,43,7,17;192,102,25,43,7,18;253,102,36,43,7,30;314,102,23,43,7,17;375,102,44,43,7,38;436,102,36,43,7,30;497,102,35,43,7,28;558,102,35,43,7,29;619,102,35,43,7,29;680,102,27,43,7,21;741,102,30,43,7,24;802,102,30,43,7,23;863,102,35,43,7,29;924,102,35,43,7,28;9,150,46,43,7,39;70,150,37,43,7,30;131,150,35,43,7,28;192,150,31,43,7,24;253,150,13,43,1,0;314,150,36,43,7,29;375,150,36,43,7,29;436,150,36,43,7,29;497,150,36,43,7,29;558,150,36,43,7,29;619,150,36,43,7,29;680,150,36,43,7,29;741,150,36,43,7,29;802,150,36,43,7,29;863,150,36,43,7,29;924,150,24,43,7,18;9,198,24,43,7,18;70,198,22,43,7,15;131,198,37,43,7,30;192,198,36,43,7,29;253,198,24,43,7,18;314,198,24,43,7,18;375,198,34,43,7,27;436,198,33,43,7,26;497,198,30,43,7,24;558,198,24,43,7,18;619,198,20,43,7,14;680,198,27,43,7,21;741,198,26,43,7,20;802,198,26,43,7,20;863,198,43,43,7,36;924,198,36,43,7,29;9,246,36,43,7,29;70,246,44,43,7,37;131,246,35,43,7,28;192,246,39,43,7,32;253,246,33,43,7,27;314,246,26,43,7,20;375,246,26,43,7,20;436,246,27,43,7,20;497,246,24,43,7,18"];
    }
    return self;
}

- (EasyGlyph*)fontObjWithSize:(NSInteger)size
{
    switch (size) {
        case 12:
            return fontWithSize12;
        case 17:
            return fontWithSize17;
        case 25:
            return fontWithSize25;
        default:
            return fontWithSize12;
    }
}

-(void)setFontForView:(UIImageView*)srcView WithSize:(NSInteger)size WithString:(NSString*)text
{
    UIImage* pFontImage = [[[FontManager sharedInstance] fontObjWithSize:size] writeTextToTexture:text];     
    
    CGRect newBounds = srcView.bounds;
    newBounds.size = pFontImage.size;
    
    srcView.image = pFontImage;                                                                                                                            
    srcView.bounds = newBounds;
}


@end
